package club.banyuan.demo.demo5;

import java.io.*;

/**
 * @author edz
 * @version 1.0
 * @date 2020/12/17 下午2:53
 */
public class CopyFolderDemo {
    public static void main(String[] args) throws IOException {
        File srcFolder = new File("/Users/edz/Desktop/2010");
        String srcFolderName = srcFolder.getName();
//        System.out.println(srcFolderName);2010
        File destFolder = new File("javaSE_Basic",srcFolderName);
//        System.out.println(destFolder.exists());
//        加号是拼接，逗号才是下级目录

        if(!destFolder.exists()){
            destFolder.mkdir();
        }
        File[] listFiles = srcFolder.listFiles();
//        System.out.println(listFiles);
        for (File srcFile : listFiles) {
            String srcFileName = srcFile.getName();
//            System.out.println(srcFileName);
            File destFile=new File(destFolder,srcFileName);
//            System.out.println(destFile);
            copyFile(srcFile,destFile);
        }
    }

    private static void copyFile(File srcFile, File destFile) throws IOException {
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));

        byte[] bys=new byte[1024];
        int len;
        while ((len=bis.read(bys))!=-1){
            bos.write(bys,0,len);
        }
        bos.close();
        bis.close();

    }
}

